#include "Widget.h"

Widget::Widget(const int& width, const int& height){

	//inicializo las dimensiones
	this->width = width;
	this->height = height;

	//inicializo los handlers
	this->clickHandlerFunc = NULL;
	this->mouseInHandlerFunc = NULL;
	this->mouseOutHandlerFunc = NULL;
	this->keyDownHandlerFunc = NULL;
	this->focusHandlerFunc = NULL;
	this->lostFocusHandlerFunc = NULL;

	//establezco el color de fondo por default
	this->setBGColor(DEFAULT_BG_COLOR_R, DEFAULT_BG_COLOR_G, DEFAULT_BG_COLOR_B);
}

Widget::Widget(){
	this->width = 0;
	this->height = 0;

	//inicializo los handlers
	this->clickHandlerFunc = NULL;
	this->mouseInHandlerFunc = NULL;
	this->mouseOutHandlerFunc = NULL;
	this->keyDownHandlerFunc = NULL;
	this->focusHandlerFunc = NULL;
	this->lostFocusHandlerFunc = NULL;

	//establezco el color de fondo por default
	this->setBGColor(DEFAULT_BG_COLOR_R, DEFAULT_BG_COLOR_G, DEFAULT_BG_COLOR_B);
}

int const Widget::getHeight(){
	return this->height;
}

void Widget::setHeight(const int& height){
	this->height = height;
}

int const Widget::getWidth(){
	return this->width;
}

void Widget::setWidth(const int& width){
	this->width = width;
}

int const Widget::getX(){
	return this->x;
}

void Widget::setX(const int& x){
	this->x = x;
}

int const Widget::getY(){
	return this->y;
}

void Widget::setY(const int& y){
	this->y = y;
}

void Widget::setClickHandler(CLICK_HANDLER_FUNC clickHandlerFunc){
	this->clickHandlerFunc = clickHandlerFunc;
}

void Widget::setMouseInHandler(MOUSE_IN_HANDLER_FUNC mouseInHandlerFunc){
	this->mouseInHandlerFunc = mouseInHandlerFunc;
}

void Widget::setMouseOutHandler(MOUSE_OUT_HANDLER_FUNC mouseOutHandlerFunc){
	this->mouseOutHandlerFunc = mouseOutHandlerFunc;
}

void Widget::setKeyDownHandler(KEY_DOWN_HANDLER_FUNC keyDownHandlerFunc){
	this->keyDownHandlerFunc = keyDownHandlerFunc;
}

void Widget::setFocusHandler(FOCUS_HANDLER_FUNC focusHandlerFunc){
	this->focusHandlerFunc = focusHandlerFunc;
}

void Widget::setLostFocusHandler(LOST_FOCUS_HANDLER_FUNC lostFocusHandlerFunc){
	this->lostFocusHandlerFunc = lostFocusHandlerFunc;
}

void Widget::click(const SDL_Event& event){

	//si corresponde, capturo el foco
	if(this->isFocusable())
		SDLControls::getInstance()->setFocus(this, event);

	if(this->clickHandlerFunc != NULL)
		this->clickHandlerFunc(this, event);
}

void Widget::mouseIn(const SDL_Event& event){

	if(this->mouseInHandlerFunc != NULL)
		this->mouseInHandlerFunc(this, event);
}

void Widget::mouseOut(const SDL_Event& event){

	if(this->mouseOutHandlerFunc != NULL)
		this->mouseOutHandlerFunc(this, event);
}

void Widget::keyDown(const SDL_Event& event){

	if(this->keyDownHandlerFunc != NULL)
		this->keyDownHandlerFunc(this, event);
}

void Widget::focus(const SDL_Event& event){

	if(this->focusHandlerFunc != NULL)
		this->focusHandlerFunc(this, event);
}

void Widget::lostFocus(const SDL_Event& event){

	if(this->lostFocusHandlerFunc != NULL)
		this->lostFocusHandlerFunc(this, event);
}

bool Widget::isWidgetContainer(){
	return false;
}

bool Widget::includesPoint(const int& x, const int& y){
	return this->getX() <= x && x <= ( this->getX() + this->getWidth() ) && this->getY() <= y && y <= ( this->getY() + this->getHeight() );
}

void Widget::setOnScreenWidgetId(const int& onScreenWidgetId){
	this->onScreenWidgetId = onScreenWidgetId;
}

int const Widget::getOnScreenWidgetId(){
	return this->onScreenWidgetId;
}

void Widget::setBGColor(const int& red, const int& green, const int& blue){
	this->bgColor.r = red;
	this->bgColor.g = green;
	this->bgColor.b = blue;
}

void Widget::setBGColor(const SDL_Color& color){
	this->bgColor = color;
}

SDL_Color Widget::getBGColor(){
	return this->bgColor;
}

bool Widget::isFocusable(){
	return false;
}

Widget::~Widget(){}

